Ext.onReady(function(){
	Ext.QuickTips.init();
	var fp = new Ext.FormPanel({
		layout:'fit',
        fileUpload: true,
        width: 500,
        frame: true,
        autoHeight: true,
        bodyStyle: 'padding: 0px 2px 0 2px;',
        defaults: {
            anchor: '95%',
            msgTarget: 'side'
        },
        items: [
        	{
            xtype: 'fileuploadfield',
            id: 'form-file',
            name: 'resource',
            buttonText: '',
            tooltip:'选择文件',
            buttonCfg: {
                iconCls: 'upload-icon'
            }
        }]
    });
	var bar = new Ext.Toolbar([fp,{
						text : "上传",
						icon : '../../resource/upload.png',
						handler:uploadFile
					}]);
	var nameRecord=Ext.data.Record.create([
		{name:'id'},
		{name:'name'},
		{name:'size',type:'int'},
		{name:'download',type:'int'},
		{name:'upload_time'},
		{name:'type'},
		{name:'course_id'}]);
	var reader=new Ext.data.ArrayReader({},nameRecord);
//	var proxy=new Ext.data.MemoryProxy(data);
	/*var store=new Ext.data.Store({
		reader:reader,
		proxy:proxy,
		autoLoad:true
	});*/
	var store = new Ext.data.JsonStore({
        root: 'data',
        totalProperty: 'totalcount',
        fields: [
        	'id','name','size','download','upload_time','type','course_id'
        ],
        proxy: new Ext.data.HttpProxy({
            url: 'getResourcesAction'
        }),
        autoLoad:{params:{start:0,limit:50}}
    });
	var grid_panel=new Ext.grid.GridPanel({
		columnLines: true,
		store:store,
		applyTo:'resource_div',
      //  title:'ExtJS.com',
        store: store,
        region:'center',
        loadMask: true,
         sm: new Ext.grid.RowSelectionModel({
            singleSelect:true
        }),
		viewConfig:{
			forceFit:true
		},
		tbar:[bar],
		bbar:[
			new Ext.PagingToolbar({
				store:store,
				pageSize:50,
				displayInfo: true,
				displayMsg:'第 {0} 条到 {1} 条,一共 {2} 条',
				emptyMsg:'没有记录'
			})
		],
		columns:[
		{
	//		renderer: getrender,
			header:'文件名',
			dataIndex:'name',
			width:12
		},{
			header:'大小',
			dataIndex:'size',
			align:'center',
			renderer:sizerender,
			width:2
		},{
			header:'类型',
			dataIndex:'type',
			align:'center',
			width:2
		},{
			header:'下载次数',
			dataIndex:'download',
			align:'center',
			width:2
		},{
			header:'上传时间',
			dataIndex:'upload_time',
			align:'center',
			width:2
		},{
			header:'操作',
			tooltip: '下载',
			dataIndex:'id',
			align:'center',
			renderer:oprender,
			width:1
		}
		/*{
                xtype: 'actioncolumn',
                width: 1,
                align:'center',
                items: [{
                    icon   : '../../resource/download.png',  // Use a URL in the icon config
                    handler:download,
                    tooltip: '下载'
                },{
                	icon:'../../resource/delete.gif',
                	tooltip:'删除'
                }]
          }*/]
	});
	var head_panel=new Ext.Panel({
		height:115,
		region:'north',
		autoLoad:'head.html',
		applyTo:'div_head'
	});
	head_panel.load('head.html');
	new Ext.Viewport({
		layout:'border',
		items:[
		head_panel,
		grid_panel
		]
	});
	function uploadFile()
	{
		fp.form.submit({
			url:'uploadResourceAction',
			method:'POST',
			waitMsg:'正在上传请稍等...',
			success:function(){Ext.Msg.alert('提示','上传成功');fp.form.reset();},
			failure:function(){Ext.Msg.alert('提示','上传失败');}
		});
	}
	function download()
	{
		
	}
	function getrender(value,m,r)
	{
		return '<div style="height:24px;line-height:24px">'+'<img src="../../resource/test.png" />'+value+'</div>';
	}
	function sizerender(value,m,r)
	{
		var ret="";
		if(value<1024)
			ret= value+'B';
		else if(value<1024*1024)
		{
			value=value/1024;
			value=value.toFixed(2);
			ret= value+'KB';
		}
		else
		{
			value=value/(1024*1024);
			value=value.toFixed(2);
			ret= value+'M';
		}
	//	ret='<div style="line-height:24px">'+ret+'</div>';
		return ret;
	}
	function oprender(value,m,r)
	{
		return '<a href="downloadResource?resource='+value+
		'"><img ext:qtip="下载" src=../../resource/download.png style="margin-right:3px;"/></a>'+
		'<a href="deleteResource?resource='+value+'">'+
		'<img ext:qtip="删除" src="../../resource/delete.gif">'+
		'</a>';
	}
});